package pprz.components;

import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Label;
import java.awt.Scrollbar;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.beans.PropertyChangeEvent;

import pprz.data_components.LogPlayerDevice;
import pprz.light.LightContainer;

public class LogPlayerDevicePanel extends DevicePanel 
implements AdjustmentListener {

	static final long serialVersionUID=1;
	
	
	Button play;
	Button stop;
	Scrollbar progress;
	Label l_msg_per_sec;
	TextField t_msg_per_sec;
	
	
	public LogPlayerDevicePanel()
	{
		super();	
		allocate();
		initialize();
	}
	
	private void allocate()
	{
		play=new Button("play");
		stop=new Button("stop");
		progress=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,100);
		l_msg_per_sec=new Label("message rate (msg/sec)");
		t_msg_per_sec=new TextField();
		
	}
	
	private void initialize()
	{
		this.setLayout(new FlowLayout());
		progress.setPreferredSize(new Dimension(150, 20));
		progress.addAdjustmentListener(this);
		this.add(progress);
		
//		play.setDefaultPaint(new GradientPaint(0,0,Color.white,0,1,Color.black,false));
//		play.setMouseOverPaint(new GradientPaint(0,0,Color.white.darker(),0,1,Color.black.brighter(),false));
//		play.setPushedPaint(Color.black);
		play.addActionListener(this);
		this.add(play);
		
//		stop.setDefaultPaint(new GradientPaint(0,0,Color.white,0,1,Color.black,false));
//		stop.setMouseOverPaint(new GradientPaint(0,0,Color.white.darker(),0,1,Color.black.brighter(),false));
//		stop.setPushedPaint(Color.black);
		stop.addActionListener(this);
		this.add(stop);
		
		LightContainer lc=new LightContainer();
//		lc.setSize(50, 50);
//		lc.setBounds(new Rectangle(0,0,20,20));
		lc.setPreferredSize(new Dimension(20,20));
		this.add(lc);
		
		this.setPreferredSize(this.getLayout().preferredLayoutSize(this));
	}
	
	/* (non-Javadoc)
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	public void propertyChange(PropertyChangeEvent evt) {
		String property=evt.getPropertyName();
		super.propertyChange(evt);
		if ("progress".equals(property))
		{
			int v=((Long)evt.getNewValue()).intValue();
			System.out.println(v);
			((Scrollbar)this.getComponent(3)).setValue(((Long)evt.getNewValue()).intValue());
		}
		validate();
//		validate();
//		repaint(100);
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent arg0) {
		if (dev==null) return;
		if (arg0.getSource().equals(init))
			dev.init();
		else if (arg0.getSource().equals(play))
			((LogPlayerDevice)dev).run();
		else if (arg0.getSource().equals(stop)) {
			((LogPlayerDevice)dev).stop();
		}
	}

	public void adjustmentValueChanged(AdjustmentEvent e) {
		if (e.getSource().equals(progress)) //make sure its this panel component
		{
			if (!e.getValueIsAdjusting()) //use only final adjustment value
			{
				((LogPlayerDevice)dev).seek(e.getValue());
			}
		}
	}
	
	
	
	
}
